package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TMStat2CMchntDayMapper;
import cc.rengu.igas.momp.common.entity.TMStat2CMchntDay;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 对客户营销活动商户单日统计表数据库操作接口实现
 *
 * @author Jinan Liu
 * @date 2020/4/15 17:06
 */

public class TMStat2CMchntDayMapperImpl implements TMStat2CMchntDayMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 获取对客户营销活动商户单日统计表数据数据库操作接口实现
     *
     * @param tmStat2CMchntDay 对客户营销活动商户单日统计表
     * @return cc.rengu.igas.momp.common.entity.TMStat2CMchntDay 对客户营销活动商户单日统计表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/15 17:08
     */
    @Override
    public TMStat2CMchntDay selectToClientMcMchntDayStatistics(TMStat2CMchntDay tmStat2CMchntDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectToClientMcMchntDayStatByUsingPK");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStat2CMchntDay, tmStat2CMchntDay);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>获取营销产品信息失败!<{}>", tmStat2CMchntDay.getInstId(), iReturnCode);
            return null;
        }

        return tmStat2CMchntDay;
    }

    /**
     * 更新对客户营销活动商户单日统计表额度数据库操作接口实现
     *
     * @param tmStat2CMchntDay 对客户营销活动商户单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/23 19:04
     */
    @Override
    public int updateToClientMcMchntDayStatistics(TMStat2CMchntDay tmStat2CMchntDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateToClientMcMchntDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStat2CMchntDay, TMStat2CMchntDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新机构号<{}>在<{}>的营销活动<{}>的商户<{}>的对客户营销活动商户单日统计表数据失败!<{}>",
                    tmStat2CMchntDay.getInstId(), tmStat2CMchntDay.getTxnDate(), tmStat2CMchntDay.getMcNo(), tmStat2CMchntDay.getMchntNo(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 插入对客户营销活动商户单日统计表额度数据库操作接口实现
     *
     * @param tmStat2CMchntDay 对客户营销活动商户单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/9 23:42
     */
    @Override
    public int insertSingleToClientMcMchntDayStatistics(TMStat2CMchntDay tmStat2CMchntDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSingleToClientMcMchntDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStat2CMchntDay, TMStat2CMchntDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("插入机构号<{}>在<{}>的营销活动<{}>的商户<>的对客户营销活动商户单日统计表数据失败!<{}>",
                    tmStat2CMchntDay.getInstId(), tmStat2CMchntDay.getTxnDate(), tmStat2CMchntDay.getMcNo(), tmStat2CMchntDay.getMchntNo(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }
}
